﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace DataAccess.Operate
{
    public class DA_Report_Plan_Operate:SqlInteractionBase
    {
        private DataEntity.T_Report_Plan _objMessage = null;
        public override bool Insert(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Plan_Insert]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_Report_Plan)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Plan_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Plan_ProjectID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ProjectID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Plan_ExchangeTypeID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ExchangeTypeID));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Plan_ExchangeDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ExchangeDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Plan_ReportNO", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ReportNO));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Plan_Comm", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Comm));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Plan_ProjectDep", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ProjectDep));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Plan_Referral", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Referral));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Plan_PayRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_PayRemark));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Plan_Remark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Remark));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Plan_Audit", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Audit));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Plan::Insert::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Update(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Plan_Update]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_Report_Plan)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Plan_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@iReport_Plan_ExchangeTypeID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ExchangeTypeID));
                cmdToExecute.Parameters.Add(new SqlParameter("@daReport_Plan_ExchangeDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ExchangeDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Plan_ReportNO", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ReportNO));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Plan_Comm", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Comm));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Plan_ProjectDep", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_ProjectDep));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcReport_Plan_Referral", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 2, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Referral));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Plan_PayRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_PayRemark));
                cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Plan_Remark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, this._objMessage.Report_Plan_Remark));
            

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Plan::Update::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override bool Delete(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Plan_Delete]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Plan_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Plan::Delete::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override object SelectOne(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Plan_SelectOne]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataTable toReturn = new DataTable("t_Report_Plan");
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = new DataEntity.T_Report_Plan();
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Plan_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    this._objMessage.Report_Plan_ID = (Guid)toReturn.Rows[0]["Report_Plan_ID"];
                    this._objMessage.Report_Plan_ProjectID = (Guid)toReturn.Rows[0]["Report_Plan_ProjectID"];
                    this._objMessage.Report_Plan_ExchangeTypeID = (Int32)toReturn.Rows[0]["Report_Plan_ExchangeTypeID"];
                    this._objMessage.Report_Plan_ExchangeDate = (DateTime)toReturn.Rows[0]["Report_Plan_ExchangeDate"];
                    this._objMessage.Report_Plan_ReportNO = (string)toReturn.Rows[0]["Report_Plan_ReportNO"];
                    this._objMessage.Report_Plan_Comm = (Decimal)toReturn.Rows[0]["Report_Plan_Comm"];
                    this._objMessage.Report_Plan_ProjectDep = (Decimal)toReturn.Rows[0]["Report_Plan_ProjectDep"];
                    this._objMessage.Report_Plan_Referral = (Decimal)toReturn.Rows[0]["Report_Plan_Referral"];
                    this._objMessage.Report_Plan_PayRemark = (string)toReturn.Rows[0]["Report_Plan_PayRemark"];
                    this._objMessage.Report_Plan_Remark = (string)toReturn.Rows[0]["Report_Plan_Remark"];
                    this._objMessage.Report_Plan_Audit = (Int32)toReturn.Rows[0]["Report_Plan_Audit"];
                }
                return this._objMessage;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Plan::SelectOne::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }


        public override DataSet SelectAll()
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Report_Plan_SelectAll]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;

            try
            {

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Report_Plan::SelectAll::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }
    }
}
